package main.security;

import java.io.Serializable;

import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

import main.entities.User;
import main.interfaces.UserBusinessLocal;

@Named("UserSession")
@SessionScoped
public class UserSession implements Serializable {
	
	/**
	 * GUID for Serializable.
	 */
	private static final long serialVersionUID = 2264814783835788944L;

	@EJB(name = "UserBusiness")
	private UserBusinessLocal userEjb;
	
	private User currentUser = null;
	
	public boolean attemptSignin(String userName, String password) {
		User questionableUser = null;
		
		questionableUser = userEjb.getUserByUsername(userName);
		
		if(questionableUser != null && questionableUser.getPassword().equals(password)) {
			currentUser = questionableUser;
			return true;
		}
		
		return false;
	}
	
	public boolean isSignedIn() {
		return (currentUser != null) && (currentUser.getActiveFlag().equals("Y"));
	}
	
	public User getCurrentUser() {
		return currentUser;
	}
	
}
